//
//  NewsViewController.m
//  Corner
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 Zero_Zhou. All rights reserved.
//

#import "NewsViewController.h"
#import "MewsTableViewController.h"
#import "AppDelegate.h"
#import "ZtitleLabel.h"
#import "UIView+Frame.h"
#import "MJExtension.h"

@interface NewsViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *smallScrollview;
//标题栏
@property (weak, nonatomic) IBOutlet UIScrollView *bigScrollview;
//内容栏

@property(nonatomic,strong) NSArray *arrayList;
// </ 数据数组
@property(nonatomic,strong) ZtitleLabel *oldTitleLabel;
// </ 标签标题
@property(nonatomic,assign) CGFloat beginOffSetX;
// </ 开始位移的位置


@end

@implementation NewsViewController
#pragma mark---懒加载
-(NSArray *)arrayList{
    if (_arrayList == nil) {
        _arrayList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewsURLs.plist" ofType:nil]];
    }
    return _arrayList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showRightItem) name:@"ZZAdvertisementKey" object:nil];
    self.view.backgroundColor = [UIColor redColor];
  self.title = @"资讯";
    self.tabBarController.tabBar.backgroundColor = [UIColor colorWithRed:1.000 green:0.241 blue:0.308 alpha:1.000];
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:menuBtn];

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.bigScrollview.delegate = self;
    
    //添加子视图
    [self addController];
    //添加标题
    [self addLabel];
    
    CGFloat contentX = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    self.bigScrollview.contentSize = CGSizeMake(contentX, 0);
    self.bigScrollview.pagingEnabled = YES;
    
    //添加默认控制器
    UIViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = self.bigScrollview.bounds;
    [self.bigScrollview addSubview:vc.view];
    ZtitleLabel *label = [self.smallScrollview.subviews firstObject];
    label.scale = 1.0;
    self.bigScrollview.showsHorizontalScrollIndicator = NO;
    // Do any additional setup after loading the view.
}

//添加子控制器
-(void)addController{
    for (int i = 0; i < self.arrayList.count; i++) {
        MewsTableViewController *vc1 = [[UIStoryboard storyboardWithName:@"News" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
        vc1.title = self.arrayList[i][@"title"];
        vc1.urlString = self.arrayList[i][@"urlString"];
        [self addChildViewController:vc1];
    }
}
//添加标题
-(void)addLabel{
    for (int i = 0; i <8; i++) {
        CGFloat lblW = 70;
        CGFloat lblH = 40;
        CGFloat lblY = 0;
        CGFloat lblX = i * lblW;
        ZtitleLabel *lbl1 = [[ZtitleLabel alloc] init];
        UITableViewController *vc = self.childViewControllers[i];
        lbl1.text = vc.title;
        lbl1.frame = CGRectMake(lblX, lblY, lblW, lblH);
        lbl1.font = [UIFont fontWithName:@"HYQiHei" size:19];
        [self.smallScrollview addSubview:lbl1];
        lbl1.tag = i;
        lbl1.userInteractionEnabled = YES;
        [lbl1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblVlick:)]];
    }
    self.smallScrollview.contentSize = CGSizeMake(70 * 8, 0);
}
//标题栏点击事件
- (void)lblVlick:(UITapGestureRecognizer*)recognizer{
    ZtitleLabel *titlelable = (ZtitleLabel*)recognizer.view;
    CGFloat offsetX = titlelable.tag *self.bigScrollview.frame.size.width;
    CGFloat offsetY = self.bigScrollview.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.bigScrollview setContentOffset:offset animated:YES];
}
#pragma  mark - scrollview代理
//
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //获取索引
    NSUInteger index = scrollView.contentOffset.x/self.bigScrollview.frame.size.width;
    //滚动标题栏
    ZtitleLabel *titleLabel = (ZtitleLabel*)self.smallScrollview.subviews[index];
    CGFloat offsetx = titleLabel.center.x - self.smallScrollview.frame.size.width *0.5;
    CGFloat offsetMax = self.smallScrollview.contentSize.width - self.smallScrollview.frame.size.width;
    if (offsetx < 0) {
        offsetx = 0;
    }else if(offsetx > offsetMax){
        offsetx = offsetMax;
    }
    CGPoint offset = CGPointMake(offsetx, self.smallScrollview.contentOffset.y);
    [self.smallScrollview setContentOffset:offset animated:YES];
    //添加控制器
    MewsTableViewController *newsVC = self.childViewControllers[index];
    newsVC.index = index;
    [self.smallScrollview.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx != index) {
            ZtitleLabel *temlabel = self.smallScrollview.subviews[idx];
            temlabel.scale = 0.0;
        }
    }];
    if (newsVC.view.superview) {
        return;
    }
    newsVC.view.frame = scrollView.bounds;
    [self.bigScrollview addSubview:newsVC.view];
    }
//滚动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}
//正在滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //取出绝对值,避免最左边往右边拉时形变超过1
    CGFloat value = ABS(scrollView.contentOffset.x / scrollView.frame.size.width);
    NSUInteger leftIndex = (int)value;
    NSUInteger rightIndex = leftIndex +1;
    CGFloat scaleRight = value - leftIndex;
    CGFloat scaleLeft = 1 - scaleRight;
    ZtitleLabel *labelLeft = self.smallScrollview.subviews[leftIndex];
    labelLeft.scale = scaleLeft;
    //考虑到最后一个板块,如果右边已经没有板块了,就不在下面赋值scale了
    if (rightIndex <self.smallScrollview.subviews.count) {
        ZtitleLabel *labelRight = self.smallScrollview.subviews[rightIndex];
        labelRight.scale = scaleRight;
    }
}


- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVc.closed)
    {
        [tempAppDelegate.LeftSlideVc openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVc closeLeftView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
